# 登录注册功能测试

Write-Host "======================================" -ForegroundColor Green
Write-Host "🧪 登录注册功能测试" -ForegroundColor Green
Write-Host "======================================" -ForegroundColor Green

$backendUrl = "http://localhost:5153"
$frontendUrl = "http://localhost:3000"

# 检查服务状态
Write-Host "🔍 检查服务状态..." -ForegroundColor Blue

try {
    $backendStatus = Invoke-WebRequest -Uri "$backendUrl/swagger" -Method GET -TimeoutSec 5
    Write-Host "✅ 后端服务正常 ($backendUrl)" -ForegroundColor Green
} catch {
    Write-Host "❌ 后端服务未启动" -ForegroundColor Red
    Write-Host "请先运行启动脚本！" -ForegroundColor Yellow
    exit 1
}

try {
    $frontendStatus = Invoke-WebRequest -Uri $frontendUrl -Method GET -TimeoutSec 5
    Write-Host "✅ 前端服务正常 ($frontendUrl)" -ForegroundColor Green
} catch {
    Write-Host "⚠️ 前端服务检查失败，可能还在启动中" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "🔐 测试用户注册..." -ForegroundColor Magenta

# 生成测试用户数据
$timestamp = Get-Date -Format "HHmmss"
$testUser = @{
    Username = "testuser$timestamp"
    Email = "test$timestamp@example.com"
    Password = "Test123!@#"
    Phone = "13800138000"
} | ConvertTo-Json

Write-Host "测试数据: " -NoNewline -ForegroundColor Gray
Write-Host $testUser -ForegroundColor DarkGray

try {
    $registerResponse = Invoke-RestMethod -Uri "$backendUrl/api/auth/register" -Method POST -Body $testUser -ContentType "application/json"
    Write-Host "✅ 注册成功: $($registerResponse.Message)" -ForegroundColor Green
    $registeredUsername = ($testUser | ConvertFrom-Json).Username
} catch {
    Write-Host "❌ 注册失败: $($_.Exception.Message)" -ForegroundColor Red
    if ($_.Exception.Message -like "*400*") {
        Write-Host "💡 可能是用户已存在或验证失败" -ForegroundColor Yellow
    }
    # 使用默认用户测试登录
    $registeredUsername = "admin"
}

Write-Host ""
Write-Host "🔑 测试用户登录..." -ForegroundColor Magenta

# 登录测试
$loginData = @{
    Username = $registeredUsername
    Password = "Test123!@#"
} | ConvertTo-Json

Write-Host "登录用户: $registeredUsername" -ForegroundColor Gray

try {
    $loginResponse = Invoke-RestMethod -Uri "$backendUrl/api/auth/login" -Method POST -Body $loginData -ContentType "application/json"
    Write-Host "✅ 登录成功!" -ForegroundColor Green
    Write-Host "访问令牌: $($loginResponse.accessToken.Substring(0, 30))..." -ForegroundColor Green
    if ($loginResponse.refreshToken) {
        Write-Host "刷新令牌: $($loginResponse.refreshToken.Substring(0, 30))..." -ForegroundColor Green
    }
} catch {
    Write-Host "❌ 登录失败: $($_.Exception.Message)" -ForegroundColor Red
    
    # 尝试使用默认管理员账户
    if ($registeredUsername -ne "admin") {
        Write-Host "🔄 尝试默认管理员账户..." -ForegroundColor Yellow
        $adminLogin = @{
            Username = "admin"
            Password = "admin123"
        } | ConvertTo-Json
        
        try {
            $adminLoginResponse = Invoke-RestMethod -Uri "$backendUrl/api/auth/login" -Method POST -Body $adminLogin -ContentType "application/json"
            Write-Host "✅ 管理员登录成功!" -ForegroundColor Green
        } catch {
            Write-Host "❌ 管理员登录也失败" -ForegroundColor Red
        }
    }
}

Write-Host ""
Write-Host "⚙️ 测试配置管理API..." -ForegroundColor Cyan

try {
    $configStatus = Invoke-RestMethod -Uri "$backendUrl/api/ConfigurationMigration/status" -Method GET
    Write-Host "✅ 配置状态获取成功:" -ForegroundColor Green
    Write-Host "  - 使用集中化配置: $($configStatus.IsUsingCentralizedConfig)" -ForegroundColor Gray
    Write-Host "  - 配置提供商: $($configStatus.ConfigProvider)" -ForegroundColor Gray
    Write-Host "  - 有本地配置: $($configStatus.HasLocalConfig)" -ForegroundColor Gray
    Write-Host "  - 有云配置: $($configStatus.HasCloudConfig)" -ForegroundColor Gray
} catch {
    Write-Host "❌ 配置API测试失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""
Write-Host "🌍 浏览器访问链接:" -ForegroundColor Blue
Write-Host "  🎨 前端应用: $frontendUrl"
Write-Host "  📚 API文档: $backendUrl/swagger"
Write-Host "  🔐 认证端点: $backendUrl/api/auth"

Write-Host ""
Write-Host "📝 可用的认证端点:" -ForegroundColor Yellow
Write-Host "  POST /api/auth/register - 用户注册"
Write-Host "  POST /api/auth/login - 用户登录"
Write-Host "  POST /api/auth/refresh-token - 刷新令牌"
Write-Host "  POST /api/auth/send-sms-code - 发送短信验证码"
Write-Host "  POST /api/auth/reset-password - 重置密码"

Write-Host ""
Write-Host "✅ 测试完成！应用已就绪" -ForegroundColor Green
Write-Host "现在可以通过浏览器或API工具进行进一步测试" -ForegroundColor White
